package com.firefly.admin.monitor.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.firefly.admin.common.enums.OperationModuleEnum;
import com.firefly.admin.common.enums.OperationTypeEnum;
import com.firefly.admin.common.vo.Result;
import com.firefly.admin.monitor.entity.FyMonitorErrorOperationLog;
import com.firefly.admin.monitor.entity.FyMonitorOperationLog;
import com.firefly.admin.monitor.mapper.FyMonitorErrorOperationLogMapper;
import com.firefly.admin.monitor.mapper.FyMonitorOperationLogMapper;
import com.firefly.admin.monitor.service.FyMonitorOperationLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import com.firefly.admin.monitor.vo.MonitorOperateLogQueryVo;
import com.firefly.admin.monitor.vo.SelectResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class FyMonitorOperationLogServiceImpl implements FyMonitorOperationLogService {
    @Autowired
    private FyMonitorOperationLogMapper fyMonitorOperationLogMapper;

    @Autowired
    private FyMonitorErrorOperationLogMapper fyMonitorErrorOperationLogMapper;

    /**
     * 系统日志新增
     *
     * @param fyMonitorOperationLog 日志信息
     * @return 新增结果
     */
    @Override
    public int save(FyMonitorOperationLog fyMonitorOperationLog) {
        return fyMonitorOperationLogMapper.insert(fyMonitorOperationLog);
    }

    /**
     * 系统错误日志新增
     *
     * @param fyMonitorErrorOperationLog 错误日志信息
     * @return 新增结果
     */
    @Override
    public int save(FyMonitorErrorOperationLog fyMonitorErrorOperationLog) {
        return fyMonitorErrorOperationLogMapper.insert(fyMonitorErrorOperationLog);
    }

    /**
     * 分页查询系统日志接口
     *
     * @param monitorOperateLogQueryVo 分页查询参数
     * @return 分页列表
     */
    @Override
    public Result<Page<FyMonitorOperationLog>> getMonitorOperateLogList(MonitorOperateLogQueryVo monitorOperateLogQueryVo) {
        log.info("###########StMonitorOperationLogServiceImpl getMonitorOperateLogList param = {}", JSONObject.toJSONString(monitorOperateLogQueryVo));
        LambdaQueryWrapper<FyMonitorOperationLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(monitorOperateLogQueryVo.getOperateModule()), FyMonitorOperationLog::getOperateModule, monitorOperateLogQueryVo.getOperateModule());
        queryWrapper.eq(StringUtils.isNotBlank(monitorOperateLogQueryVo.getOperateType()), FyMonitorOperationLog::getOperateType, monitorOperateLogQueryVo.getOperateType());
        queryWrapper.eq(StringUtils.isNotBlank(monitorOperateLogQueryVo.getOperateResult()), FyMonitorOperationLog::getOperateResult, monitorOperateLogQueryVo.getOperateResult());
        queryWrapper.orderByDesc(FyMonitorOperationLog::getLogId);
        Page<FyMonitorOperationLog> page = new Page<>(monitorOperateLogQueryVo.getPage(), monitorOperateLogQueryVo.getLimit());
        Page<FyMonitorOperationLog> monitorOperationLogPage = fyMonitorOperationLogMapper.selectPage(page, queryWrapper);
        return Result.success(monitorOperationLogPage);
    }

    /**
     * 分页查询系统错误日志接口
     *
     * @param monitorOperateLogQueryVo 分页查询参数
     * @return 分页列表
     */
    @Override
    public Result<Page<FyMonitorErrorOperationLog>> getMonitorErrorOperateLogList(MonitorOperateLogQueryVo monitorOperateLogQueryVo) {
        log.info("###########StMonitorOperationLogServiceImpl getMonitorErrorOperateLogList param = {}", JSONObject.toJSONString(monitorOperateLogQueryVo));
        LambdaQueryWrapper<FyMonitorErrorOperationLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(monitorOperateLogQueryVo.getOperateModule()), FyMonitorErrorOperationLog::getOperateModule, monitorOperateLogQueryVo.getOperateModule());
        queryWrapper.eq(StringUtils.isNotBlank(monitorOperateLogQueryVo.getOperateType()), FyMonitorErrorOperationLog::getOperateType, monitorOperateLogQueryVo.getOperateType());
        queryWrapper.orderByDesc(FyMonitorErrorOperationLog::getLogId);
        Page<FyMonitorErrorOperationLog> page = new Page<>(monitorOperateLogQueryVo.getPage(), monitorOperateLogQueryVo.getLimit());
        Page<FyMonitorErrorOperationLog> monitorErrorOperationLogPage = fyMonitorErrorOperationLogMapper.selectPage(page, queryWrapper);
        return Result.success(monitorErrorOperationLogPage);
    }

    /**
     * 查询日志页面枚举下拉选接口
     *
     * @return 查询结果
     */
    @Override
    public Result<Map<String, Object>> getOperateLogSelectEnum() {
        Map<String, Object> resultMap = new HashMap<>();
        OperationModuleEnum[] moduleEnums = OperationModuleEnum.values();
        List<SelectResult> operationModuleList = new ArrayList<>();
        for (OperationModuleEnum operationModuleEnum : moduleEnums) {
            SelectResult selectResult = new SelectResult();
            selectResult.setName(operationModuleEnum.name());
            selectResult.setValue(operationModuleEnum.getModule());
            operationModuleList.add(selectResult);
        }
        resultMap.put("operationModuleList", operationModuleList);
        OperationTypeEnum[] operationTypeEnums = OperationTypeEnum.values();
        List<SelectResult> operationTypeList = new ArrayList<>();
        for (OperationTypeEnum operationTypeEnum : operationTypeEnums) {
            SelectResult selectResult = new SelectResult();
            selectResult.setName(operationTypeEnum.name());
            selectResult.setValue(operationTypeEnum.getType());
            selectResult.setRelation(operationTypeEnum.getModule());
            operationTypeList.add(selectResult);
        }
        resultMap.put("operationTypeList", operationTypeList);
        return Result.success(resultMap);
    }
}
